﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HB.MVVM
{
    /// <summary>
    /// 示例: public string str { get => GetString(); set => Set(value); }
    /// </summary>
    public class ViewModelBase<T> : ViewModelBase where T : new()
    {
        private static readonly object locker = 0;
        private static T instance = default(T);
        /// <summary>
        /// 获取单例
        /// </summary>
        public static T Instance
        {
            get
            {
                if (instance == null) //避免每次加锁判定，第一次需要
                {
                    lock (locker) // 锁定，防止多线程同时访问
                    {
                        if (instance == null) //双重判定
                        {
                            instance = new T();
                        }
                    }
                }
                return instance;
            }
            private set { }
        }
    }
    public class ViewModelBaseFody<T> : ViewModelBaseFody where T : new()
    {
        private static readonly object locker = 0;
        private static T instance = default(T);
        /// <summary>
        /// 获取单例
        /// </summary>
        public static T Instance
        {
            get
            {
                if (instance == null) //避免每次加锁判定，第一次需要
                {
                    lock (locker) // 锁定，防止多线程同时访问
                    {
                        if (instance == null) //双重判定
                        {
                            instance = new T();
                        }
                    }
                }
                return instance;
            }
            private set { }
        }
    }
}
